/* This file is part of FireEscape.
 *
 * FireEscape is the intellectual work of Phillip Cheng & Allen Park.
 * All future releases or modifications of this program must include this
 * attribuation as according to the Creative Commons BY-SA license.
 *
 * FireEscape is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * FireEscape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FireEscape.  If not, see <http://www.gnu.org/licenses/>.
 *
 * All modifications to this program should include this original statement.
 *
 * If you modify, distribute, or use the program , please contact
 * fireescape@kloudvine.com
 * http://code.google.com/p/fireescape/
 *
 * If you are interested in using FireEscape for your structure, please contact us
 * for support.
 *
 * If you do use FireEscape, please consider donating to the project.
 */
package fireescape;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.logging.*;

/**
 * Represents the route
 * @author chengP
 */
public class Route implements Serializable {

    private ArrayList<Integer> route;
    private int firstRoom;
    private ArrayList<Double> times;

    /**
     * Constructs the route
     */
    public Route() {
        route = new ArrayList<Integer>();
        times = new ArrayList<Double>();
    }

    /**
     * Adds a time to the list
     * @param iterations The number of iterations it took a bot to escape using this route
     */
    public void addTime(int iterations) {
        times.add((double) iterations);
    }

    /**
     * Returns the average of the times
     * @return The average of the times
     */
    public double retrieveAverage() {
        double sum = 0;
        for (Double d : times) {
            sum += d;
        }
        return sum / times.size();
    }

    /**
     * Returns the maximum time
     * @return The maximum number of iterations it took for a bot to escape
     */
    public double getSlowestTime() {
        double time = 0;
        for (Double d : times) {
            if (d > time) {
                time = d;
            }
        }
        return time;
    }

    /**
     * Return the navpoint exit associated with the route at the given index
     * @param index the index of the exit
     * @return the exit associated with the index
     */
    public Exit getNavPoint(int index) {
        try {
            return Map.getExit(route.get(index));
        } catch (IndexOutOfBoundsException ex) {
            Logger.getLogger("FireEscapeLog").log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Returns the number of exits in the route
     * @return The number of exits in the route
     */
    public int getRouteSize() {
        return route.size();
    }

    /**
     * Add a new waypoint to the route
     * @param e The new exit
     */
    public void addNavPoint(Exit exit) {
        route.add(exit.getExitID());
    }

    public void addNavPoint(int exitID) {
        route.add(exitID);
    }

    /**
     * Sets the starting room of the route
     * @param roomID The ID of the starting room of the route
     */
    public void setFirstRoom(int roomID) {
        firstRoom = roomID;
    }

    /**
     * Returns the ID of the starting room of the route
     * @return The ID of the starting room of the route
     */
    public int getFirstRoom() {
        return firstRoom;
    }

    /**
     * Removes a section from the route
     * @param start The first index to be removed
     * @param end The last index (the first index to not be removed)
     */
    public void removeSection(int start, int end) {
        try {
            ArrayList<Integer> newRoute = new ArrayList<Integer>();
            for (int i = 0; i < route.size(); i++) {
                if (i < start || i >= end) {
                    newRoute.add(route.get(i));
                }
            }
            route = newRoute;
        } catch (IndexOutOfBoundsException ex) {
            Logger.getLogger("FireEscapeLog").log(Level.SEVERE, null, ex);
        }
    }
}
